package cn.iocoder.yudao.module.system.controller.admin.menuweixin;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static java.util.Collections.singleton;

import cn.iocoder.yudao.module.system.controller.admin.menuweixin.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.menuweixin.MenuWeixinDO;
import cn.iocoder.yudao.module.system.service.menuweixin.MenuWeixinService;

@Tag(name = "管理后台 - 微信菜单")
@RestController
@RequestMapping("/system/menu-weixin")
@Validated
public class MenuWeixinController {

    @Resource
    private MenuWeixinService menuWeixinService;

    @PostMapping("/create")
    @Operation(summary = "创建微信菜单")
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:create')")
    public CommonResult<Long> createMenuWeixin(@Valid @RequestBody MenuWeixinSaveReqVO createReqVO) {
        return success(menuWeixinService.createMenuWeixin(createReqVO));
    }
    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色，主要用于前端的下拉选项")
    public CommonResult<List<MenuWeixinRespVO>> getSimpleRoleList() {
        List<MenuWeixinDO> list = menuWeixinService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
        list.sort(Comparator.comparing(MenuWeixinDO::getSort));
        return success(BeanUtils.toBean(list, MenuWeixinRespVO.class));
    }
    @PutMapping("/update")
    @Operation(summary = "更新微信菜单")
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:update')")
    public CommonResult<Boolean> updateMenuWeixin(@Valid @RequestBody MenuWeixinSaveReqVO updateReqVO) {
        menuWeixinService.updateMenuWeixin(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信菜单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:delete')")
    public CommonResult<Boolean> deleteMenuWeixin(@RequestParam("id") Long id) {
        menuWeixinService.deleteMenuWeixin(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得微信菜单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:query')")
    public CommonResult<MenuWeixinRespVO> getMenuWeixin(@RequestParam("id") Long id) {
        MenuWeixinDO menuWeixin = menuWeixinService.getMenuWeixin(id);
        return success(BeanUtils.toBean(menuWeixin, MenuWeixinRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得微信菜单列表")
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:query')")
    public CommonResult<List<MenuWeixinRespVO>> getMenuWeixinList(@Valid MenuWeixinListReqVO listReqVO) {
        List<MenuWeixinDO> list = menuWeixinService.getMenuWeixinList(listReqVO);
        return success(BeanUtils.toBean(list, MenuWeixinRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信菜单 Excel")
    @PreAuthorize("@ss.hasPermission('system:menu-weixin:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMenuWeixinExcel(@Valid MenuWeixinListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<MenuWeixinDO> list = menuWeixinService.getMenuWeixinList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "微信菜单.xls", "数据", MenuWeixinRespVO.class,
                        BeanUtils.toBean(list, MenuWeixinRespVO.class));
    }

}